//
//  SelectRepairRegionDetailViewController.m
//  customer
//
//  Created by Holothurian on 2018/11/4.
//  Copyright © 2018年 yinlong. All rights reserved.
//

#import "SelectRepairRegionDetailViewController.h"
#import "OneKeyRepairViewController.h"
#import "SelectRepairDetailRegionTableCell.h"

static NSString *CellIdentifier = @"CellIdentifier";
@interface SelectRepairRegionDetailViewController ()<UITableViewDelegate,UITableViewDataSource>


@end

@implementation SelectRepairRegionDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = self.subRepairModel.parts_name?:@"";
    self.tableView.frame = CGRectMake(0, NAVIGATION_BAR_HEIGHT, F_DEVICE_W, F_DEVICE_H - NAVIGATION_BAR_HEIGHT - bottomPadding(0));
    [self.tableView registerClass:[SelectRepairDetailRegionTableCell class] forCellReuseIdentifier:CellIdentifier];
    [self loadData];
    
}


- (void)loadData{
    [self showLoading];
    AutoWeakSelf
    [[QYNetManager sharedManager]POST:kGetMaintailPartss parameters:@{@"parent_id":self.subRepairModel.parts_id?:@""} andApiTyp:CustomerApiTypGetOrders SuccessBlock:^(id responseObject) {
        [weakSelf hideLoading];
        if (self.dataList.count) {
            [self.dataList removeAllObjects];
        }
        if ([responseObject[@"status"] isEqualToString:@"error"]) {
            [weakSelf showErrorTipWithTitle:responseObject[@"error"]];
        }else{
            NSArray *dataSource = responseObject[@"data"];
            [weakSelf.dataList addObjectsFromArray:[NSArray yy_modelArrayWithClass:[SelectRepairModel class] json:dataSource]];
            [weakSelf.tableView reloadData];
        }
        
    } Failure:^(NSError *error) {
        [weakSelf hideLoading];
        [weakSelf showErrorTipWithTitle:CustomerPoolNetStr];
    }];
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.dataList count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SelectRepairDetailRegionTableCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    SelectRepairModel *model = self.dataList[indexPath.row];
    cell.nameLabel.text = model.parts_name;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50.f;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSArray *subViews = self.navigationController.viewControllers;
    SelectRepairModel *model = self.dataList[indexPath.row];
    for (BaseViewController *baseVC in subViews) {
        if ([baseVC isKindOfClass:[OneKeyRepairViewController class]]) {
            baseVC.isPop = YES;
            baseVC.popArray = @[self.fatherRepairModel,self.subRepairModel,model];
            [self.navigationController popToViewController:baseVC animated:YES];
        }
    }
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
